package itsm.isperp.framework.web.response;

import itsm.isperp.framework.utils.JsonBuilder;

import java.io.Serializable;

/**
 * 
 * 
 * 用于返回给页面的结果数据信息
 * 
 * @author lizx
 * @date 2013-6-25
 * 
 */
public class ResultMessage implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String JSON_SUCCESS = "{success:true}";
	public static final String JSON_SUCCESS_FALSE = "{success:false}";

	String msg;

	public boolean success;

	public Object data;

	public ResultMessage() {
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public static ResultMessage success(Object data) {
		return new ResultMessage(true, data);
	}

	public static ResultMessage failure() {
		return new ResultMessage(false, null);
	}

	public static ResultMessage failure(Object data) {
		return new ResultMessage(false, data);
	}

	public ResultMessage(boolean success, Object data) {
		this.success = success;
		this.data = data;
	}

	public boolean isSuccess() {
		return success;
	}

	protected ResultMessage(boolean success) {
		this.success = success;
	}

	protected ResultMessage(boolean success, String msg) {
		this.success = success;
		this.msg = msg;
	}

	protected ResultMessage(boolean success, String msg, Object data) {
		this.success = success;
		this.msg = msg;
		this.data = data;
	}

	public static ResultMessage newInstance(boolean success) {
		return new ResultMessage(success);
	}

	public static ResultMessage newInstance(boolean success, String msg) {
		return new ResultMessage(success, msg);
	}

	public static ResultMessage newInstance(boolean success, String msg,
			Object data) {
		return new ResultMessage(success, msg, data);
	}

	public static ResultMessage success() {
		return new ResultMessage(true);
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Override
	public String toString() {
		return JsonBuilder.buildNormalBinder().toJson(this);
	}
}
